package com.guo.hui.shiro;

import java.util.HashMap;
import java.util.List;

import org.apache.tomcat.jni.Mmap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.guo.hui.controller.BaseController;
import com.guo.hui.dao.shiro.TpUserMapper;
import com.guo.hui.enumtype.DelFlag;
import com.guo.hui.model.shiro.TpUser;
import com.guo.hui.service.TpUserShiroService;
import com.guo.hui.util.UtilJson;

@Controller
@RequestMapping("/blogadmin")
public class AdminController extends BaseController {

	@Autowired
	private TpUserShiroService userService;
	@Autowired
	private TpUserMapper userDao;

	@RequestMapping("/index")
	public String index() {
		return "blog/admin/index";
		// return "test/testnav2";
	}

	@RequestMapping("/userperson")
	public String userperson(Model mm) {
		Integer id = this.getUserInfo().getId();
		if (id != null) {
			TpUser tp = userDao.selectById(id);
			mm.addAttribute("tp", tp);
		}
		return "blog/admin/userperson";
	}

	@RequestMapping("/user")
	public String user() {
		return "blog/admin/user";
	}

	@RequestMapping("/useradd")
	public String useradd() {
		return "blog/admin/useradd";
	}

	@ResponseBody
	@RequestMapping("/usersave")
	public String usersave(@RequestParam String user) {
		TpUser tp = (TpUser) UtilJson.fromJson(user, TpUser.class);
		userService.saveUser(tp);
		return "ok";
	}

	@ResponseBody
	@RequestMapping("/userdata")
	public String userdata(Model mm) {
		List<TpUser> list = userService.selectByMap(new HashMap<String, Object>());
		return UtilJson.toJson(list);
	}

	@ResponseBody
	@RequestMapping("/userdelete")
	public String userdelete(@RequestParam int id) {
		TpUser tp = new TpUser();
		tp.setId(id);
		tp.setStatus(DelFlag.LOSE.toString());
		userService.updateUser(tp);
		return "ok";
	}

	@RequestMapping("/update")
	public String update(Model mm, @RequestParam int id) {
		TpUser tp = userDao.selectById(id);
		mm.addAttribute("tp", tp);
		return "blog/admin/update";
	}

	@ResponseBody
	@RequestMapping("/userupdate")
	public String userupdate(@RequestParam String tp) {
		TpUser user = (TpUser) UtilJson.fromJson(tp, TpUser.class);
		userService.updateUser(user);
		return "ok";
	}

}
